<?php
require('OrdenCompra.class.php');
require('ParametroGeneral.class.php');
require('Proveedor.class.php');
require('Cliente.class.php');

$modo = (isset($_GET['modo'])) ? $_GET['modo']:0;
switch($modo){
    case 1: //Consultar Grid
        if(isset($_GET['modo']) == 1){    
            $tipoProducto   = $_GET['tipoProducto'];
            $page           = $_POST['page'];
            $rp             = $_POST['rp'];
            $sortname       = $_POST['sortname'];
            $sortorder      = $_POST['sortorder'];
            $query          = $_POST['query'];
            $letter_pressed = isset($_POST['letter_pressed'])?$_POST['letter_pressed']:null;
            $qtype          = $_POST['qtype'];
            $obj            = new OrdenCompra();
            $obj->consultar_grid($page, $rp, $sortname, $sortorder, $query, $letter_pressed, $qtype, $tipoProducto);
        }
        break;
    case 2: //Insertar
        if(isset($_POST['submit'])){
            $proId  = $_POST['proId'];
            $usuId  = $_POST['usuId'];
            $fecAct = date("Ymd");
            $horAct = date("His");
            // idElemento+"#"+prodId+"#"+cant+"#"+prod+"#"+accion+"#"+iocId; |        
            $odc = htmlspecialchars(trim($_POST['elementos']));
            
            $par = new ParametroGeneral();
            $codOdc = $par->getIncrementoParametro("CODODC");
            
            $itemOdc = explode("|", $odc);

            for ($i = 0, $count = count($itemOdc); $i < $count; $i++) {
                if(strlen(trim($itemOdc[$i])) >0){
                    $arrayIOC[$i] = (explode("#", $itemOdc[$i]));
                }				
            }
            $array[0] = $proId;
            $array[1] = $codOdc;
            $array[2] = $fecAct;
            $array[3] = $horAct;
            $array[4] = $usuId;
            $array[5] = $arrayIOC;
	    $obj    = new OrdenCompra();
            
            if ( $obj->insertar($array) == true)  echo 1;
            else echo 0;

        }
        break;
    case 3: //Modificar
        if(isset($_POST['submit'])){
            $odcId  = $_POST['id'];
            $usuId  = $_POST['usuId'];
            $fecAct = date("Ymd");
            $horAct = date("His");
            // idElemento+"#"+prodId+"#"+cant+"#"+prod+"#"+accion+"#"+iocId; |        
            $odc = htmlspecialchars(trim($_POST['elementos']));
            
            $itemOdc = explode("|", $odc);

            for ($i = 0, $count = count($itemOdc); $i < $count; $i++) {
                if(strlen(trim($itemOdc[$i])) >0){
                    $arrayIOC[$i] = (explode("#", $itemOdc[$i]));
                }				
            }
            $array[0] = $odcId;
            $array[1] = $arrayIOC;
	    $obj    = new OrdenCompra();
            
            if ( $obj->actualizar($array) == true)  echo 1;
                else echo 0;   
           
        }        
        break;
    case 4://Eliminar
        if(isset($_POST['submit'])){
            $id     = $_POST['id'];
            $obj    = new OrdenCompra();
            if ( $obj->eliminar($id) == true) echo 1;
            else echo 0;
        }             
        break;
    case 5: //Consultar Autocompletar Proveedor
        $obj    = new Proveedor();
        $obj->getItemsProveedor();
        break;
    case 6: //Consulta los Datos del Proveedor y los ítems de Ordenes de Compra
        $proId  = $_GET['proId'];
        $odcId  = $_GET['odcId'];
        $obj    = new OrdenCompra();
        $obj->getOdc($proId, $odcId);
        break;
    case 7: //Consultar los Datos de la Orden de Compra con sus Items
        $odcId  = $_GET['odcId'];
        $obj    = new OrdenCompra();
        $obj->getOdc(null, $odcId);
        break;    
    case 8: //Nota de Recepción
        $array[0] = $_POST['iocId'];
        $array[1] = $_POST['cantR'];
        $array[2] = $_POST['cantP'];
        $array[3] = $_POST['mapId'];
        $array[4] = $_POST['usuId'];
        $array[5] = date("Ymd");
        $array[6] = date("His");
        $obj    = new OrdenCompra();
        if ($obj->actualizarCantItemsOdc($array)) echo 1;
        else echo 0;
        break;
    case 9: //Consultar Autocompletar Cliente
        $obj    = new Cliente();
        $obj->getItemsCliente();
        break;  
    default:
        echo "Debe seleccionar un modo: \n1. Consultar Grid\n2. Insertar\n3. Modificar\n4. Eliminar";
        break;
}
?>
